#include "TimerOne.h"
#include "Arduino.h"

#include "IRremote.h"

extern "C" {
#include "irmpconfig.h"
#include "irmp.h"
}

#define US (1000000 / F_INTERRUPTS)

static uint16_t
bitsrevervse (uint16_t x, uint8_t len);

uint8_t receive = 1;
uint8_t last_start_bit = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Moieen!");
  pinMode(IRMP_BIT, INPUT);                                                                                                                                                                                
  delay(20); /* make sure the greeting string is out before starting */                                                                                                                                                            
  Timer1.initialize(US);                                                                                                                                                                                                           
  // set the interrupt callback function                                                                                                                                                                                           
  Timer1.attachInterrupt(timerinterrupt);    
}

//IRsend irsend;
IRsend irsend;
IRMP_DATA irmp_data;

void loop() {
    /*for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0x02FCB04F, 32); // Sony TV power code
      delay(3000);
    }*/
    if(irmp_get_data(&irmp_data)) {
        Serial.print(irmp_data.protocol);
        Serial.print(":");
        Serial.print(irmp_data.address, HEX);                                                                                                                                                                                          
        Serial.print(":");
        Serial.print(irmp_data.command, HEX);                                                                                                                                                                                          
        Serial.print(":");
        Serial.print(irmp_data.flags, HEX);                                                                                                                                                                                            
        Serial.println("");
        
        cli();
        uint32_t raw = 0;
        raw = bitsrevervse(irmp_data.address, 16);
        raw <<= 16;
        raw |= (bitsrevervse(irmp_data.command, 8) << 8);
        raw |= 0x00FF & ~(bitsrevervse(irmp_data.command, 8));
        irsend.sendNEC(raw, 32);
        Serial.print("Relaying NEC RAW Data: ");
        Serial.println(raw, HEX);
        sei();
    }
}

/*
uint32_t getNECRawData(IRMP_DATA* data) {
    uint32_t raw = 0;
    raw = bitsrevervse(data->address, 16);
    raw <<= 16;
    raw |= bitsrevervse(data->command, 8);
    raw <<= 8;
    raw |= ~(bitsrevervse(data->command, 8));
    return raw;
}*/


static uint16_t
bitsrevervse (uint16_t x, uint8_t len)
{
    uint16_t    xx = 0;
    
    while(len)
    {
        xx <<= 1;
        if (x & 1)
        {
            xx |= 1;
        }
        x >>= 1;
        len--;
    }
    return xx;
}

void timerinterrupt()
{
    if (receive) {
      uint8_t ir_detected;
      irmp_ISR(&ir_detected);
      if (last_start_bit != ir_detected) {
        Serial.println(ir_detected);
        last_start_bit = ir_detected;
      }
    }
}



void toggle_led(int pin) {
    digitalWrite(pin, 1-digitalRead(3));                                                                                                                                                                                           
}    
